package com.stp.controller;

import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.stp.houbb.CustomSensitiveWordReplace;
import com.stp.util.SensitiveFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class SensitiveController {
    private static Logger logger = LoggerFactory.getLogger(SensitiveController.class);

    @Autowired
    private SensitiveWordBs wordBs;

    @GetMapping("/sensitive/{keyword}")
    public String sensitive(@PathVariable("keyword") String keyword){
        //String s = sensitiveFilter.replaceSensitiveWord(keyword);
        String searchKey = "傻逼h";
        String placeholder = "***";
        //非法敏感词汇判断
        //SensitiveFilter filter = SensitiveFilter.getInstance();
        String s = SensitiveFilter.replaceSensitiveWord(searchKey, 1, placeholder);
        System.out.println(s);
        int n = SensitiveFilter.CheckSensitiveWord(searchKey,0,2);
        //存在非法字符
        if(n > 0){
            logger.info("这个人输入了非法字符--> {},不知道他到底要查什么~ userid--> {}",searchKey,1);
        }
        return s;
    }

    @GetMapping("/bs/{text}")
    public String bs(@PathVariable("text") String text){
        System.out.println(wordBs.replace(text));
        String replace = wordBs.replace(text, new CustomSensitiveWordReplace());
        return replace;
    }

    // 两种方式都可以
    public static void main(String[] args) throws IOException {
        String searchKey = "傻逼h";
        String placeholder = "***";
        //非法敏感词汇判断
        //SensitiveFilter filter = SensitiveFilter.getInstance();
        String s = SensitiveFilter.replaceSensitiveWord(searchKey, 1, placeholder);
        System.out.println(s);
        int n = SensitiveFilter.CheckSensitiveWord(searchKey,0,2);
        //存在非法字符
        if(n > 0){
            logger.info("这个人输入了非法字符--> {},不知道他到底要查什么~ userid--> {}",searchKey,1);
        }
    }
}
